﻿using System;
using System.IO;
using Newtonsoft.Json;

namespace AIC_Notification
{
    class Configuration
    {
        private const string CONFIG_FILE_NAME = "AIC_Notification-Config.json";

        public string Office365UserName { get; set; }
        public string Office365UserPassword { get; set; }
        public string TrelloAppKey { get; set; }
        public string SOF_TrelloUserToken { get; set; }
        public string Guidance_TrelloUserToken { get; set; }
        public string TeacherFileName { get; set; }
        public string SOF_StudentFileName { get; set; }
        public string Guidance_StudentFileName { get; set; }
        public string SchoolFileName { get; set; }
        public string ProgramFilter { get; set; }
        public int    YearFilter { get; set; }
        public string CityFilter { get; set; }
        public string StudentFilter { get; set; }
        public DateTime SOF_Student_LastModified { get; set; }
        public DateTime Guidance_Student_LastModified { get; set; }
        public bool IsSetDateTime { get; set; } = false;
        public DateTime SetDateTime { get; set; }

        public bool Load()
        {
            try
            {
                string ConfigFile = Global.InputDirectory + CONFIG_FILE_NAME;
                using (StreamReader file = File.OpenText(ConfigFile))
                {
                    JsonSerializer serializer = new JsonSerializer();
                    Global.config = (Configuration)serializer.Deserialize(file, typeof(Configuration));
                }
            }
            catch
            {
                Global.SysUtil.Log("Failed to read from configuration file", 2);
                return false;
            }

            return true;
        }

        public bool Save()
        {
            try
            {
                // Write Students Object to Json
                string ConfigFile = Global.InputDirectory + CONFIG_FILE_NAME;
                using (StreamWriter sw = new StreamWriter(ConfigFile))
                {
                    string temp = JsonConvert.SerializeObject(Global.config, Formatting.Indented);
                    sw.WriteLine(temp);
                }
            }
            catch
            {
                Global.SysUtil.Log("Failed to write to configuration file", 2);
                return false;
            }

            return true;
        }
    }
}
